package game;

import game.player.Player;
import gui.GUIController;
import techLayer.Database;
/*
 * This class is where all the logic for the 4 in row game.
 */

public class GameManager {

    private GUIController guiController;
    private GameGrid gameGrid;
    private MarkerQueue queue;
    private FourInRowRules rules;
    private Player player1, player2;
    private int currentPlayer = 1; //Hosten börjar alltid
    /*
     * GameManager constructor.
     * Creates a new GameGrid, GUIController, MarkerQueue and FourInRowRules.
     */

    public GameManager() {
        gameGrid = new GameGrid();
        guiController = new GUIController(this);
        queue = new MarkerQueue();
        rules = new FourInRowRules(getGrid());
    }
    /*
     * This method returns the gameGrid.
     */

    public GameGrid getGameGrid() {
        return gameGrid;
    }
    /*
     * This method returns the markerQueue
     */

    public MarkerQueue getMarkerQueue() {
        return queue;
    }
    /*
     * This method returns the current gamegrid matrix.
     */

    public int[][] getGrid() {
        return gameGrid.getGameGrid();
    }
    /*
     * This method adds the two players.
     */

    public void addPlayers(Player player1, Player player2) {
        this.player1 = player1;
        this.player2 = player2;
    }
    /*
     * This method returns the next player.
     */

    public int getNextPlayer(int currentPlayer) {
        return (currentPlayer % 2) + 1;
    }

    /*
     * This method returns the next players move.
     */
    public int getPlayerNextMove(int playerId) {
        if (playerId == 1) {
            return player1.nextMove();
        } else {
            return player2.nextMove();
        }
    }
    /*
     * This method returns the current players name.
     */

    public String getPlayerName(int playerId) {
        if (playerId == 1) {
            return player1.getName();
        } else {
            return player2.getName();
        }
    }

    /*
     * This method returns the current player
     */
    public int getCurrentPlayer() {
        return currentPlayer;
    }
    /*
     * This method returns the name of player 1.
     */

    public String getPlayer1Name() {
        return player1.getName();
    }
    /*
     * This method returns the name of player 2.
     */

    public String getPlayer2Name() {
        return player2.getName();
    }
    /*
     * This method starts a new thread thats plays the game
     * Asks the current player for the next move while the board is not full.
     * If there is a winner in the game or if it's a draw, a dialog will appear.
     */

    public void playGame() {
       currentPlayer = getCurrentPlayer(); 
		 new Thread(new Runnable() {
            
            @Override
            public void run() {
                while (!rules.boardIsFull()) {
                    guiController.updateCurrentPlayer();

                    int column = getPlayerNextMove(currentPlayer);
                    if (gameGrid.placeDisc(column, currentPlayer)) {
                        guiController.updateGameBoard();
                        
                        if (rules.boardIsFull()) {
                            guiController.showDrawnDialog();
                            break;
                        } else if (rules.gameWon(column)) {
                            guiController.showWinnerDialog(
                                    getPlayerName(currentPlayer));
									 player1.cleanUpPlayer();
									 player2.cleanUpPlayer();
                            if(player1.getPlayer() == currentPlayer) //Let the Host update the scores
                                updateDBScores();
                            break;
                        }
                        currentPlayer = getNextPlayer(currentPlayer);
                    }
                }
                guiController.updateGameBoard();
            }
        }).start();
    }
    private void updateDBScores(){
        Database db = new Database();
        String winner = getPlayerName(currentPlayer);
        String loser = getPlayerName((currentPlayer%2)+1);
        
        if(!db.playerExists(winner))
            db.addPlayer(winner);
        if(!db.playerExists(loser))
            db.addPlayer(loser);
                
        db.addWin(winner);
        db.addLoss(loser);
        db.close();
    }
}
